package com.homegoods.designer.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.designer.R;
import com.homegoods.designer.databinding.ActivityDesignerBinding;
import com.homegoods.designer.presenter.DesignerPresenter;
import com.homegoods.designer.ui.view.IDesignView;
import com.homegoods.resource.pullDownMenuAdapter.SwitchConditionAdapter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.resource.util.MultiPullDownMenuProxy;
import com.kedll.library.KBaseApplication;
import com.kedll.library.base.KRefreshListActivity;
import com.kedll.library.location.entity.LocationEntity;
import com.kedll.library.util.RxBus;

import java.util.Arrays;

import io.reactivex.android.schedulers.AndroidSchedulers;

import static com.homegoods.designer.presenter.DesignerPresenter.C_ALL;
import static com.homegoods.designer.presenter.DesignerPresenter.C_AREA;
import static com.homegoods.designer.presenter.DesignerPresenter.C_PRICE;
import static com.homegoods.designer.presenter.DesignerPresenter.C_TYPE;


/**
 * @author QiQi
 */
@Route(path = ARouterPath.PATH_DESIGNER)
public class DesignerActivity extends KRefreshListActivity<ActivityDesignerBinding, DesignerPresenter> implements IDesignView {

    /**
     * 设计师分类
     */
    private final static String[] TYPE = new String[]{
            "设计师类型",
            "设计机构(公司)",
            "独立自主设计师"
    };


    /**
     * 报价
     */
    private final static String[] PRICE = new String[]{
            "设计报价",
            "100元/m²以下",
            "100-199元/m²",
            "200-299元/m²",
            "300-500元/m²",
            "500元/m²以上"
    };

    /**
     * 综合排序
     */
    private final static String[] ALL = new String[]{
            "综合排序",
            "星级排序"
    };
    private MultiPullDownMenuProxy multiPullDownMenuProxy;

    private SwitchConditionAdapter
            //区域条件选择器
            areaAdapter,
    //类型条件选择器
    typeAdapter,
    //报价条件选择器
    priceAdapter,
    //综合条件选择器
    allAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_designer;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mBinding.setPresenter(mPresenter);

        //初始化菜单面板切换代理
        multiPullDownMenuProxy = new MultiPullDownMenuProxy(mBinding.pullDownMenu, R.color.color_a541c9, R.color.color_333333);

        //注册菜单面板
        multiPullDownMenuProxy.registerMenu(C_AREA, mBinding.llArea);
        multiPullDownMenuProxy.registerMenu(C_TYPE, mBinding.llType);
        multiPullDownMenuProxy.registerMenu(C_PRICE, mBinding.llPrice);
        multiPullDownMenuProxy.registerMenu(C_ALL, mBinding.llAll);

        LocationEntity entity = KBaseApplication.getInstance().getLocationEntity();
        if (entity != null) {
            mBinding.tvArea.setText(entity.getCity());
        }
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent() {
        super.initEvent();
        RxBus.getDefault()
                .toFlowable(Bundle.class)
                .compose(bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(bundle -> {
                    String city = bundle.getString("result");
                    mBinding.tvArea.setText(city);
                    mPresenter.setCondition(city);
                });
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("找设计师");
        setTopRightButton("申请设计师", v -> ARouter.getInstance()
                .build(ARouterPath.PATH_DESIGNER_APPLY)
                .navigation());

    }

    @Override
    protected DesignerPresenter getPresenter() {
        return new DesignerPresenter(this);
    }

    @Override
    public void conditionSelector(int condition) {
        switchSelector(condition);
    }

    private void switchSelector(int condition) {
        //设置当前选择的面板
        multiPullDownMenuProxy.setCurrentCondition(condition);
        //切换面板对应的数据绑定
        switch (condition) {
            case C_AREA:
//                if (areaAdapter == null) {
//                    areaAdapter = new SwitchConditionAdapter(this, new PullDownClickListener(mPresenter, AREA, mBinding.tvArea));
//                    areaAdapter.setData(Arrays.asList(AREA));
//                }
//                multiPullDownMenuProxy.switchAttachView(areaAdapter, mBinding.llArea);
                ARouter.getInstance()
                        .build(ARouterPath.PATH_HOT_CITY)
                        .navigation();

                break;
            case C_TYPE:
                if (typeAdapter == null) {
                    typeAdapter = new SwitchConditionAdapter(this, new PullDownClickListener(mPresenter, TYPE, mBinding.tvDesignType));
                    typeAdapter.setData(Arrays.asList(TYPE));
                }
                multiPullDownMenuProxy.switchAttachView(typeAdapter, mBinding.llType);
                break;
            case C_PRICE:
                if (priceAdapter == null) {
                    priceAdapter = new SwitchConditionAdapter(this, new PullDownClickListener(mPresenter, PRICE, mBinding.tvDesignPrice));
                    priceAdapter.setData(Arrays.asList(PRICE));
                }

                multiPullDownMenuProxy.switchAttachView(priceAdapter, mBinding.llPrice);
                break;
            case C_ALL:
                if (allAdapter == null) {
                    allAdapter = new SwitchConditionAdapter(this, new PullDownClickListener(mPresenter, ALL, mBinding.tvAll));
                    allAdapter.setData(Arrays.asList(ALL));
                }
                multiPullDownMenuProxy.switchAttachView(allAdapter, mBinding.llAll);
                break;
            default:
        }
    }


    /**
     * 下拉菜单选中回调
     */
    private static final class PullDownClickListener implements SwitchConditionAdapter.OnItemClickListener {

        private DesignerPresenter designerPresenter;
        private String[] data;
        private TextView conditionView;

        PullDownClickListener(DesignerPresenter designerPresenter, String[] data, TextView conditionView) {
            this.designerPresenter = designerPresenter;
            this.data = data;
            this.conditionView = conditionView;
        }

        @Override
        public void onItemClick(int position) {
            conditionView.setText(data[position]);
            //TODO 按照分类过滤查找数据
            designerPresenter.setCondition(data[position]);
        }
    }
}
